<template>
  <div class="user-info-view">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>
          用户信息管理
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            style="margin-left: 690px"
            @click="addBtnClickEvent"
          ></el-button>
        </span>
      </div>
      <div>
        <el-table
          :data="$store.state.userInfo.curUserInfos"
          height="500"
          border
          style="width: 100%; text-align: center"
          @row-click="handleCurUserInfoTableRowClick"
        >
          <el-table-column prop="name" label="姓名" width="200">
          </el-table-column>
          <el-table-column prop="phoneNumber" label="电话" width="200">
          </el-table-column>
          <el-table-column prop="positionName" label="职位" width="200">
          </el-table-column>
          <el-table-column prop="stateName" label="状态" width="200">
          </el-table-column>
          <el-table-column prop="id" label="操作" width="100">
            <template slot-scope="{ row }">
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="确定删除？"
                @confirm="handleDeleteConfirm(row.id)"
              >
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  @click.stop="deleteUserInfo"
                  slot="reference"
                >
                </el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="curPageNum"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="$store.state.userInfo.usersLength"
        >
        </el-pagination>
      </div>
      <el-dialog title="用户信息" :visible.sync="curUserInfoDialogVisible">
        <el-form :model="curUserInfo">
          <el-form-item label="用户姓名" :label-width="formLabelWidth">
            <el-input v-model="curUserInfo.name" autocomplete="off"> </el-input>
          </el-form-item>
          <el-form-item label="账号用户名" :label-width="formLabelWidth">
            <el-input v-model="curUserInfo.username" autocomplete="off">
            </el-input>
          </el-form-item>
          <el-form-item label="用户电话" :label-width="formLabelWidth">
            <el-input v-model="curUserInfo.phoneNumber" autocomplete="off">
            </el-input>
          </el-form-item>
          <el-form-item label="身份证号码" :label-width="formLabelWidth">
            <el-input v-model="curUserInfo.idNum" autocomplete="off">
            </el-input>
          </el-form-item>
          <el-form-item label="职位" :label-width="formLabelWidth">
            <el-select
              v-model="curUserInfo.positionId"
              placeholder="请选择职位"
            >
              <el-option
                v-for="position in $store.state.position.positions"
                :label="position.name"
                :value="position.id"
                :key="position.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态" :label-width="formLabelWidth">
            <el-select v-model="curUserInfo.state" placeholder="请选择状态">
              <el-option label="离职" value="0"></el-option>
              <el-option label="在职" value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="curUserInfoDialogVisible = false">
            取 消
          </el-button>
          <el-button type="primary" @click="subUserInfo"> 提交 </el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "UserInfoView",
  created() {
    let token = this.$store.state.user.jwt_token;
    // 获取初始table中的用户信息
    this.$store.dispatch("getCurUserInfos", {
      pageSize: this.pageSize,
      curPageNum: this.curPageNum,
      token: token,
      success(resp) {},
      error(resp) {},
    });
    // 获取总用户信息数量
    this.$store.dispatch("getUserInfosLength", token);
    // 获取职位信息
    this.$store.dispatch("getPositions", {
      token: token,
    });
  },
  methods: {
    // 处理每页数据展示量变化
    handleSizeChange(size) {
      this.pageSize = size;
      this.$store.dispatch("getCurUserInfos", {
        pageSize: this.pageSize,
        curPageNum: this.curPageNum,
        token: this.$store.state.user.jwt_token,
        success(resp) {},
        error(resp) {},
      });
    },
    // 处理页码变化
    handleCurrentChange(curPageNum) {
      let outer = this;
      this.curPageNum = curPageNum;
      this.$store.dispatch("getCurUserInfos", {
        pageSize: outer.pageSize,
        curPageNum: outer.curPageNum,
        token: outer.$store.state.user.jwt_token,
        success(resp) {},
        error(resp) {},
      });
    },
    // 通过职位id获取职位对应的名称
    getPositionName(pid) {
      return this.$store.commit("getPositionNameById", pid);
    },
    // 处理 table 每一行的点击事件，会弹出对应的用户信息修改对话框
    handleCurUserInfoTableRowClick(row) {
      this.curUserInfo = row;
      this.curUserInfoDialogVisible = true;
      this.optionFlag = 1;
      row.state = row.state.toString();
    },
    // 提交待修改用户信息
    updateUserInfo() {
      this.curUserInfoDialogVisible = false;
      let outer = this;
      this.$store.dispatch("updateUserInfo", {
        userInfo: this.curUserInfo,
        token: this.$store.state.user.jwt_token,
        success(resp) {
          if (resp.data.code == 200) {
            outer.$message({
              message: "修改用户信息成功",
              type: "success",
            });
            outer.curUserInfo = {};
            outer.curUserInfoDialogVisible = false;
          }
        },
        error(resp) {
          console.log(resp);
        },
      });
    },
    // 提交新增用户信息
    addUserInfo() {
      let outer = this;
      this.$store.dispatch("addUserInfo", {
        userInfo: this.curUserInfo,
        token: this.$store.state.user.jwt_token,
        success(resp) {
          if (resp.data.code == 200) {
            outer.$message({
              message: "添加用户信息成功",
              type: "success",
            });
            outer.curUserInfoDialogVisible = false;
            // 记得更新table数据，这里为了方便直接调用后端api，重新获取信息
            outer.handleSizeChange(outer.pageSize);
            // 别忘了更新table长度
            outer.$store.dispatch(
              "getUserInfosLength",
              outer.$store.state.user.jwt_token
            );
          } else {
            outer.$message({
              type: "error",
              message: "添加用户信息失败：" + resp.data.message,
            });
          }
        },
        error(resp) {
          console.log(resp);
        },
      });
    },
    // 提交对话框中的信息，根据 optionFlag 选择是 1 修改 还是 2 新增
    subUserInfo() {
      if (this.optionFlag == 1) {
        this.updateUserInfo();
      } else if (this.optionFlag == 2) {
        this.addUserInfo();
      }
    },
    // 这里需要一个函数配和 @click.stop 防止事件冒泡
    deleteUserInfo() {},
    // 处理确认删除事件（提交待删除用户信息对应的id）
    handleDeleteConfirm(id) {
      let outer = this;
      this.$store.dispatch("deleteUserInfoById", {
        id: id,
        token: this.$store.state.user.jwt_token,
        success(resp) {
          if (resp.data.code == 200) {
            outer.$message({
              message: "删除用户信息成功",
              type: "success",
            });
          }
          outer.handleSizeChange(outer.pageSize);
        },
        error(resp) {},
      });
    },
    // 添加图标点击事件
    addBtnClickEvent() {
      this.optionFlag = 2;
      // 默认职位 1，状态 1，其他为空
      this.curUserInfo = {
        positionId: 1,
        // 这里是字符串的 1
        state: "1",
      };
      this.curUserInfoDialogVisible = true;
    },
  },
  data() {
    return {
      curPageNum: 1,
      pageSize: 8,
      pageSizes: [3, 5, 8, 10, 15, 20],
      curUserInfo: {},
      curUserInfoDialogVisible: false,
      formLabelWidth: "120px",
      confirmDeleteVisible: false,
      optionFlag: 1,
    };
  },
};
</script>

<style scoped>
el-table-column {
  text-align: center;
}
</style>
